package com.example.shopingcenter.util;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shopingcenter.R;
import com.example.shopingcenter.activity.ShoppingDetalsActivity;

import java.util.List;

public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ImageViewHolder> {
    private final Context context;
    private final List<Integer> imageList;

    public ViewPagerAdapter(Context context, List<Integer> imageList) {
        this.context = context;
        this.imageList = imageList;
    }


    @NonNull
    @Override
    public ViewPagerAdapter.ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.image_item, parent, false);
        return new ImageViewHolder(view);

    }


    @Override
    public void onBindViewHolder(@NonNull ViewPagerAdapter.ImageViewHolder holder, int position) {
        holder.imageView.setImageResource(imageList.get(position));


        holder.itemView.setOnClickListener(v -> {
            // 根据位置跳转页面


                    Intent intent = new Intent(context, ShoppingDetalsActivity.class);
                    intent.putExtra("goods_id",position+1);
                    context.startActivity(intent);



        });
    }


    @Override
    public int getItemCount() {
        return imageList.size();
    }

    public static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}
